
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  These are the four essential freedoms with GNU GPL software:
 *  1: freedom to run the program, for any purpose
 *  2: freedom to study how the program works, and change it to make it do what you wish
 *  3: freedom to redistribute copies to help your Free Software friends
 *  4: freedom to distribute copies of your modified versions to your Free Software friends
 *   ,           ,
 *  /             \
 * ((__-^^-,-^^-__))
 * `-_---'  `---_-'
 *  `--|o`   'o|--'
 *      \  `  /
 *       ): :(
 *       :o_o:
 *        "-"
 *
 * SPDX-License-Identifier: GPL-3.0+
 * License-Filename: LICENSE
 */

%{

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "lang.tab.h"

%}

%option warn
%option noyywrap
%option noinput
%option nounput
%option yylineno

ISTR	[^\\\"]|\\.|\\\n
STR	\"({ISTR}*)\"

CCS	\/\*[^\*]*\*+([^\*\/][^\*]*\*+)*\/
CCS0	\/\*
CCS1	\*\/
CCE	\/\/[^\n]*

%%

{CCS}				{ /* c-comment style */ /* lexer does update yylineno */ }
{CCS0}				{ /* start of c comment but no end of c comment */ }
{CCS1} 				{ /* end of c comment but no start of c comment */ }
{CCE}				{ /* c++ comment style */ /* lexer does update yylineno */ }

[\f ]+				{ /* skip form feed chars and spaces */ }
[\t]				{ /* skip tabs */ }
[\n]				{ /* skip new line */ /* lexer does update yylineno */ }
[\r]				{ /* skip carriage return */ }

"else"				{ return (T_ELSE); }
"goto"				{ return (T_GOTO); }
"if"				{ return (T_IF); }
"int"				{ return (T_INT); }
"return"			{ return (T_RETURN); }
"void"				{ return (T_VOID); }
"+"				{ return (T_ADD); }
"-"				{ return (T_SUB); }
"*"				{ return (T_MUL); }
"/"				{ return (T_DIV); }
"<"				{ return (T_LT); }
"<="				{ return (T_LTE); }
">"				{ return (T_GT); }
">="				{ return (T_GTE); }
"=="				{ return (T_EQ); }
"!="				{ return (T_NEQ); }
"!"				{ return (T_NOT): }
"="				{ return (T_ASSIGN); }
";"				{ return (T_SEMI); }
","				{ return (T_COMMA); }
"("				{ return (T_ROPEN); }
")"				{ return (T_RCLOSE); }
"["				{ return (T_SOPEN); }
"]"				{ return (T_SCLOSE); }
"{"				{ return (T_COPEN); }
"}"				{ return (T_CCLOSE); }
[a-zA-Z]+			{ yylval.string = strdup(yytext); return (T_ID); }
[0-9]+				{ yylval.intnum = atoi(yytext); return (T_NUM); }
.				{ fprintf(stderr, "unhandled: `%c'\n", yytext[0]); }

%%

/* end. */
